{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name=\"top\"></a><img src=\"source/SpinalHDL.png\" alt=\"SpinalHDL based on Scala\" style=\"width:320px;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before running Spinal HDL code, be sure to load SpinalHDL Libraries  \n",
    "**Note** : This may be a little slow when the first time load, please wait a moment to download Lib from remote.)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val path = System.getProperty(\"user.dir\") + \"/source/load-spinal.sc\"\n",
    "interp.load.module(ammonite.ops.Path(java.nio.file.FileSystems.getDefault().getPath(path)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DataType\n",
    "\n",
    "- Boolean\n",
    "- Byte\n",
    "- Short\n",
    "- Int\n",
    "- Long  //64bit\n",
    "- Float //32bit\n",
    "- Double//64bit \n",
    "- BigInt      \n",
    "- BigDecimal "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Epsilon in Double\n",
    "\n",
    "**precious depends on exponent** \n",
    "\n",
    "- FloatError  : `pow(2, exponent) * 1/pow(2,23))`\n",
    "- DoubleError : `pow(2, exponent) * 1/pow(2,52))`\n",
    "\n",
    "when exponent = 0, we got Epsilon  \n",
    "- FloatEps  :   `1/pow(2,23) = pow(2,-23) = 1.1920928955078125E-7`\n",
    "- DoubleEps :   `1/pow(2,52) = pow(2,-52) = 2.220446049250313E-16`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scala.math.pow(2,-23)\n",
    "scala.math.pow(2,-52)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "## Float\n",
    "**1 sign, 8 bits exponent, 23 bits mantissa**  \n",
    "```scala\n",
    "sign * 2^(2^exponent) * mantissa(2^23-1)  \n",
    "absMaxValue = ±1 * (mantissa52bit << 127 )  = ±pow(2, 127) * (pow(2,23)-1)/pow(2,23))\n",
    "absminValue =  1 * (mantissa52bit >> 127 )  =  pow(2,-128) * (pow(2,23)-1)/pow(2,23))\n",
    "```\n",
    " \n",
    "## Double\n",
    "**1 sign, 11 bits exponent, 52 bits mantissa**  \n",
    "```scala\n",
    "sign * 2^(2^exponent) * mantissa(2^52-1)  \n",
    "absMaxValue = ±1 * (mantissa52bit << 1024 )  = ±pow(2, 1023) * (pow(2,52)-1)/pow(2,52))\n",
    "absminValue =  1 * (mantissa52bit >> 1024 )  =  pow(2,-1024) * (pow(2,52)-1)/pow(2,52))  \n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scala.math.pow(2,-50)\n",
    "scala.math.pow(2,-51)\n",
    "scala.math.pow(2,-52)\n",
    "scala.math.pow(2,-62)\n",
    "scala.math.pow(2,-100) \n",
    "scala.math.pow(2,-100) + scala.math.pow(2,-100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.12.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
